package com.queue.array;

public class ArrayQueue {
    // 队列长度
    private int maxSize;
    // 队头指标
    private int front;
    // 队尾指标
    private int rear;
    // 队列数组
    private int[] array;

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.array = new int[maxSize];
        rear = -1;
        front = -1;
    }

    // 判断队列是否满
    private boolean isFull() {
        return rear == maxSize - 1;
    }

    // 添加数据
    public void addData(int data) {
        if(isFull()) {
            throw new RuntimeException("队列已满，无法添加数据");
        }
        rear++;
        this.array[rear] = data;
    }

    // 判断队列是否为空
    private boolean isEmpty() {
        return this.rear == this.front;
    }

    // 取出数据
    public int takeOutData() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空，无法取出数据");
        }
        front++;
        return this.array[front];
    }

    // 显示队列
    public void show() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空，无法打印队列");
        }
        for(int i = this.front + 1; i <= this.rear; i++) {
            System.out.printf("%d ", this.array[i]);
        }
    }

    // 返回队列头
    public int peekHead() {
        if(isEmpty()) {
            throw new RuntimeException("队列为空，无法返回队列头");
        }
        return this.array[this.front + 1];
    }
}
